package cn.yun.web.servletContext;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
/*
ServletContext对象:
1.概念:代表整个web应用,可以和程序的容器(服务器)来通信
2.获取方法:
       1.通过request对象获取  request.getServletContext();
       2.通过HttpServlet获取  this.getServletContext();
 3.功能
       1.获取MIME类型
            概念:在互联网通信过程中定义的一种文件数据类型
            格式: 大类型/小类型  例如: text/html,image/jpeg
            获取方法: String  getMimeType(String file)
       2.域对象:共享数据
            1.setAttribute(String name,Object value)
            2.getAttribute(String name)
            3.removeAttribute(String name)

            ServletContext对象作用范围:所有用户所有请求的数据

       3.获取文件的真实(服务器)路径
         String getRealPath(String path):
*/
@WebServlet("/servletContextDemo1")
public class ServletContextDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.通过request对象获取
        ServletContext servletContext1 = request.getServletContext();
        //2.通过HttpServlet获取
        ServletContext servletContext2 = this.getServletContext();
        System.out.println(servletContext1);//org.apache.catalina.core.ApplicationContextFacade@5d5ca34d
        System.out.println(servletContext2);//org.apache.catalina.core.ApplicationContextFacade@5d5ca34d
        System.out.println(servletContext1==servletContext2);//true



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
    }

}
